package com.sunsy.authserver.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunsy.authserver.beans.R;
import com.sunsy.authserver.beans.dto.*;
import com.sunsy.authserver.beans.po.UserAppRelation;
import com.sunsy.authserver.beans.po.UserTenantRelation;
import com.sunsy.authserver.dao.UserAppRelationDao;
import com.sunsy.authserver.dao.UserTenantRelationDao;
import com.sunsy.authserver.service.*;
import com.sunsy.authserver.util.MD5;
import com.sunsy.authserver.wrapper.AppClientWrapper;
import com.sunsy.authserver.wrapper.TenantWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("admin")
public class AdminController {

    private UniformUserService userDao;
    private UserStatusService userStatusService;
    private TenantService tenantDao;
    private AppClientService appDao;
    private TenantWrapper tenantWrapper = TenantWrapper.build();
    private AppClientWrapper appWrapper = AppClientWrapper.build();
    private UserTenantRelationService userTenantRelationDao;
    private UserAppRelationService userAppRelationDao;

    @Autowired
    public void setUserTenantRelationDao(UserTenantRelationService userTenantRelationDao) {
        this.userTenantRelationDao = userTenantRelationDao;
    }

    @Autowired
    public void setUserAppRelationDao(UserAppRelationService userAppRelationDao) {
        this.userAppRelationDao = userAppRelationDao;
    }

    @Autowired
    public void setUserStatusDao(UserStatusService userStatusService) {
        this.userStatusService = userStatusService;
    }

    @Autowired
    public void setUserDao(UniformUserService userDao) {
        this.userDao = userDao;
    }

    @Autowired
    public void setTenantDao(TenantService tenantDao) {
        this.tenantDao = tenantDao;
    }

    @Autowired
    public void setAppDao(AppClientService appDao) {
        this.appDao = appDao;
    }

    @PostMapping("user")
    @ApiOperation("用户管理-添加用户")
    public R insertUser(@Valid @RequestBody UniformUserDTO dto) {
        return R.ok(userDao.insert(dto));
    }

    @PutMapping("user")
    @ApiOperation("修改用户")
    public R updateUser(@RequestBody UniformUserDTO dto) {
        return R.ok(userDao.adminUpdate(dto));
    }

    @GetMapping("user")
    @ApiOperation("获取用户列表")
    public R getUserList(Page page, UserQueryDto dto) {
        return R.ok(userDao.getUserList(page, dto));
    }

    @DeleteMapping("user")
    @ApiOperation("删除用户")
    public R delUser(@RequestParam("id") Serializable id) {
        return R.ok(userDao.removeById(id));
    }

    @GetMapping("status")
    @ApiOperation("获取用户状态列表")
    public R getUserStatus() {
        return R.ok(userStatusService.list());
    }

    @PostMapping("tenant")
    @ApiOperation("租户管理-添加租户")
    public R insertTenant(@Valid @RequestBody TenantDTO dto) {
        return R.ok(tenantDao.save(tenantWrapper.dto2PO(dto)));
    }

    @PutMapping("tenant")
    @ApiOperation("修改租户")
    public R updateTenant(@RequestBody TenantDTO dto) {
        return R.ok(tenantDao.updateById(tenantWrapper.dto2PO(dto)));
    }

    @GetMapping("tenant")
    @ApiOperation("获取租户列表")
    public R getTenantList(Page page, TenantDTO dto) {
        return R.ok(tenantWrapper.poPage2VO(tenantDao.page(page, tenantWrapper.dto2QWrapper(dto))));
    }

//    @DeleteMapping("tenant")
//    @ApiOperation("删除用户")
//    public R delTenant(@RequestParam("id") Serializable id) {
//        return R.ok(tenantDao.removeById(id));
//    }

    @PostMapping("appClient")
    @ApiOperation("添加应用")
    public R insertApp(@Valid @RequestBody AppClientDTO dto) {
        return R.ok(appDao.save(appWrapper.dto2PO(dto)));
    }

    @PutMapping("appClient")
    @ApiOperation("更新应用信息")
    public R updateApp(@RequestBody AppClientDTO dto) {
        return R.ok(appDao.updateById(appWrapper.dto2PO(dto)));
    }

    @GetMapping("appClient")
    @ApiOperation("获取应用列表")
    public R getAppList(Page page, AppClientDTO dto) {
        return R.ok(appWrapper.poPage2VO(appDao.page(page, appWrapper.dto2QWrapper(dto))));
    }

    @GetMapping("getSecret")
    @ApiOperation("获取一个应用密钥")
    public R getAppSecret() {
        return R.ok(MD5.toMD5(UUID.randomUUID().toString()));
    }


    @PostMapping("addTenantUser")
    public R addTenantUser(@RequestBody TenantUserDto dto) {
//        删除旧的关系表
        userTenantRelationDao.delByTenantId(dto.getTenantId());
        List<UserTenantRelation> list = dto.getUserIds().stream().map(it -> {
            UserTenantRelation utr = new UserTenantRelation();
            utr.setTenantId(dto.getTenantId());
            utr.setUserId(it);
            return utr;
        }).collect(Collectors.toList());
        return R.ok(userTenantRelationDao.saveBatch(list));
    }

    @PostMapping("addAppUser")
    public R addAppUser(@RequestBody AppUserDto dto) {
        userAppRelationDao.delByAppId(dto.getAppId());
        List<UserAppRelation> list = dto.getUserIds().stream().map(it -> {
            UserAppRelation uar = new UserAppRelation();
            uar.setUserId(it);
            uar.setAppId(dto.getAppId());
            return uar;
        }).collect(Collectors.toList());
        return R.ok(userAppRelationDao.saveBatch(list));
    }

    @GetMapping("/{id}/{type}")
    public R getUserList(@PathVariable("type") Integer type, @PathVariable("id") String id) {
        switch (type) {
            case 1:
                return R.ok(userTenantRelationDao.getUserList(id));
            case 2:
                return R.ok(userAppRelationDao.getUserList(Long.valueOf(id)));
        }
        return R.failed();
    }


}
